package com.wx.auth.servlet;

import com.wx.auth.utils.AuthUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;

/**
 * 使用微信用户信息直接登录，无需注册和绑定
 * @Author: lijunwei
 * @Time: 2018/6/23 16:26
 */
@WebServlet("/wxLogin")
public class LoginServlet extends HttpServlet {


    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("--------------@WebServlet(\"/wxLogin\")------------"+"1111");
        String backUrl = "http://iekuang16.s3.luyouxia.net:20675/callBack";
        //第一步 :  重定向这个地址,主要是用来  让用户同意授权,来获取code   code的有效期为5分钟
        String url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid="+ AuthUtil.APPID
                + "&redirect_uri="+ URLEncoder.encode(backUrl)
                + "&response_type=code"
                + "&scope=snsapi_userinfo"
                + "&state=STATE#wechat_redirect";
        System.out.println("------url----001--"+url);
        //传输不会改变地址  ,不会改变地址栏上显示的地址,参数只能设置在代码中
      resp.sendRedirect(url);
    }
}
